package source.events.action;

import source.model.DataHandler;
import source.model.GameObject;
import source.model.Unit;
import source.model.Item;
import source.model.type.*;
import source.view.MainScreen;
import source.model.Useable;
import source.model.ObjectID;

public class UseItem implements Action {
	
	public UseItem( ) {
		
	}

	public ActionResult execute(DataHandler dh) {
		GameObject selected = dh.getCurrentSelection();
		
		if (selected.objectType() != GameObjectType.UNIT) {
			MainScreen.writeToConsole("Current selection is not a unit");
			return ActionResult.IMPOSSIBLE;
		}
		
		Unit unit = (Unit)selected;
		if(unit.getMissionFlag())
		{
			MainScreen.writeToConsole("Current selection is on a mission");
			return ActionResult.IMPOSSIBLE;
		}
		Item item = unit.getItem();
		if ( item == null ) 
		{
			MainScreen.writeToConsole("Current unit is not carrying an item");
		    return ActionResult.IMPOSSIBLE;
		}
		else if (!item.isUseableItem()) {
			MainScreen.writeToConsole("Item is not a useable item type.");
			return ActionResult.IMPOSSIBLE;
		}
				
		Action toDo = ((Useable)item).use();
		
		return toDo.execute(dh);
	}
}
